{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.9 \u7528SWIG\u5305\u88c5C\u4ee3\u7801\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8ba9\u4f60\u5199\u7684C\u4ee3\u7801\u4f5c\u4e3a\u4e00\u4e2aC\u6269\u5c55\u6a21\u5757\u6765\u8bbf\u95ee\uff0c\u60f3\u901a\u8fc7\u4f7f\u7528 Swig\u5305\u88c5\u751f\u6210\u5668 \u6765\u5b8c\u6210\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Swig\u901a\u8fc7\u89e3\u6790C\u5934\u6587\u4ef6\u5e76\u81ea\u52a8\u521b\u5efa\u6269\u5c55\u4ee3\u7801\u6765\u64cd\u4f5c\u3002\n\u8981\u4f7f\u7528\u5b83\uff0c\u4f60\u5148\u8981\u6709\u4e00\u4e2aC\u5934\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u793a\u4f8b\u7684\u5934\u6587\u4ef6\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "/* sample.h */\n\n#include <math.h>\nextern int gcd(int, int);\nextern int in_mandel(double x0, double y0, int n);\nextern int divide(int a, int b, int *remainder);\nextern double avg(double *a, int n);\n\ntypedef struct Point {\n    double x,y;\n} Point;\n\nextern double distance(Point *p1, Point *p2);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u4f60\u6709\u4e86\u8fd9\u4e2a\u5934\u6587\u4ef6\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u7f16\u5199\u4e00\u4e2aSwig\u201d\u63a5\u53e3\u201d\u6587\u4ef6\u3002\n\u6309\u7167\u7ea6\u5b9a\uff0c\u8fd9\u4e9b\u6587\u4ef6\u4ee5\u201d.i\u201d\u540e\u7f00\u5e76\u4e14\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "// sample.i - Swig interface\n%module sample\n%{\n#include \"sample.h\"\n%}\n\n/* Customizations */\n%extend Point {\n    /* Constructor for Point objects */\n    Point(double x, double y) {\n        Point *p = (Point *) malloc(sizeof(Point));\n        p->x = x;\n        p->y = y;\n        return p;\n   };\n};\n\n/* Map int *remainder as an output argument */\n%include typemaps.i\n%apply int *OUTPUT { int * remainder };\n\n/* Map the argument pattern (double *a, int n) to arrays */\n%typemap(in) (double *a, int n)(Py_buffer view) {\n  view.obj = NULL;\n  if (PyObject_GetBuffer($input, &view, PyBUF_ANY_CONTIGUOUS | PyBUF_FORMAT) == -1) {\n    SWIG_fail;\n  }\n  if (strcmp(view.format,\"d\") != 0) {\n    PyErr_SetString(PyExc_TypeError, \"Expected an array of doubles\");\n    SWIG_fail;\n  }\n  $1 = (double *) view.buf;\n  $2 = view.len / sizeof(double);\n}\n\n%typemap(freearg) (double *a, int n) {\n  if (view$argnum.obj) {\n    PyBuffer_Release(&view$argnum);\n  }\n}\n\n/* C declarations to be included in the extension module */\n\nextern int gcd(int, int);\nextern int in_mandel(double x0, double y0, int n);\nextern int divide(int a, int b, int *remainder);\nextern double avg(double *a, int n);\n\ntypedef struct Point {\n    double x,y;\n} Point;\n\nextern double distance(Point *p1, Point *p2);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u4f60\u5199\u597d\u4e86\u63a5\u53e3\u6587\u4ef6\uff0c\u5c31\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u5de5\u5177\u4e2d\u8c03\u7528Swig\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % swig -python -py3 sample.i\nbash %"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "swig\u7684\u8f93\u51fa\u5c31\u662f\u4e24\u4e2a\u6587\u4ef6\uff0csample_wrap.c\u548csample.py\u3002\n\u540e\u9762\u7684\u6587\u4ef6\u5c31\u662f\u7528\u6237\u9700\u8981\u5bfc\u5165\u7684\u3002\n\u800csample_wrap.c\u6587\u4ef6\u662f\u9700\u8981\u88ab\u7f16\u8bd1\u5230\u540d\u53eb _sample \u7684\u652f\u6301\u6a21\u5757\u7684C\u4ee3\u7801\u3002\n\u8fd9\u4e2a\u53ef\u4ee5\u901a\u8fc7\u8ddf\u666e\u901a\u6269\u5c55\u6a21\u5757\u4e00\u6837\u7684\u6280\u672f\u6765\u5b8c\u6210\u3002\n\u4f8b\u5982\uff0c\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a\u5982\u4e0b\u6240\u793a\u7684 setup.py \u6587\u4ef6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# setup.py\nfrom distutils.core import setup, Extension\n\nsetup(name='sample',\n      py_modules=['sample.py'],\n      ext_modules=[\n        Extension('_sample',\n                  ['sample_wrap.c'],\n                  include_dirs = [],\n                  define_macros = [],\n\n                  undef_macros = [],\n                  library_dirs = [],\n                  libraries = ['sample']\n                  )\n        ]\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u7f16\u8bd1\u548c\u6d4b\u8bd5\uff0c\u5728setup.py\u4e0a\u6267\u884cpython3\uff0c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 setup.py build_ext --inplace\nrunning build_ext\nbuilding '_sample' extension\ngcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes\n-I/usr/local/include/python3.3m -c sample_wrap.c\n -o build/temp.macosx-10.6-x86_64-3.3/sample_wrap.o\nsample_wrap.c: In function \u2018SWIG_InitializeModule\u2019:\nsample_wrap.c:3589: warning: statement with no effect\ngcc -bundle -undefined dynamic_lookup build/temp.macosx-10.6-x86_64-3.3/sample.o\n build/temp.macosx-10.6-x86_64-3.3/sample_wrap.o -o _sample.so -lsample\nbash %"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4e00\u5207\u6b63\u5e38\u7684\u8bdd\uff0c\u4f60\u4f1a\u53d1\u73b0\u4f60\u5c31\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u4f7f\u7528\u751f\u6210\u7684C\u6269\u5c55\u6a21\u5757\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sample\nsample.gcd(42,8)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.divide(42,8)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p1 = sample.Point(2,3)\np2 = sample.Point(4,5)\nsample.distance(p1,p2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p1.x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p1.y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import array\na = array.array('d',[1,2,3])\nsample.avg(a)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Swig\u662fPython\u5386\u53f2\u4e2d\u6784\u5efa\u6269\u5c55\u6a21\u5757\u7684\u6700\u53e4\u8001\u7684\u5de5\u5177\u4e4b\u4e00\u3002\nSwig\u80fd\u81ea\u52a8\u5316\u5f88\u591a\u5305\u88c5\u751f\u6210\u5668\u7684\u5904\u7406\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6240\u6709Swig\u63a5\u53e3\u90fd\u4ee5\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\u7684\u4e3a\u5f00\u5934\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "%module sample\n%{\n#include \"sample.h\"\n%}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u4ec5\u4ec5\u53ea\u662f\u58f0\u660e\u4e86\u6269\u5c55\u6a21\u5757\u7684\u540d\u79f0\u5e76\u6307\u5b9a\u4e86C\u5934\u6587\u4ef6\uff0c\n\u4e3a\u4e86\u80fd\u8ba9\u7f16\u8bd1\u901a\u8fc7\u5fc5\u987b\u8981\u5305\u542b\u8fd9\u4e9b\u5934\u6587\u4ef6\uff08\u4f4d\u4e8e %{ \u548c %} \u7684\u4ee3\u7801\uff09\uff0c\n\u5c06\u5b83\u4eec\u4e4b\u95f4\u590d\u5236\u7c98\u8d34\u5230\u8f93\u51fa\u4ee3\u7801\u4e2d\uff0c\u8fd9\u4e5f\u662f\u4f60\u8981\u653e\u7f6e\u6240\u6709\u5305\u542b\u6587\u4ef6\u548c\u5176\u4ed6\u7f16\u8bd1\u9700\u8981\u7684\u5b9a\u4e49\u7684\u5730\u65b9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Swig\u63a5\u53e3\u7684\u5e95\u4e0b\u90e8\u5206\u662f\u4e00\u4e2aC\u58f0\u660e\u5217\u8868\uff0c\u4f60\u9700\u8981\u5728\u6269\u5c55\u4e2d\u5305\u542b\u5b83\u3002\n\u8fd9\u901a\u5e38\u4ece\u5934\u6587\u4ef6\u4e2d\u88ab\u590d\u5236\u3002\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4ec5\u4ec5\u50cf\u4e0b\u9762\u8fd9\u6837\u76f4\u63a5\u7c98\u8d34\u5728\u5934\u6587\u4ef6\u4e2d\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "%module sample\n%{\n#include \"sample.h\"\n%}\n...\nextern int gcd(int, int);\nextern int in_mandel(double x0, double y0, int n);\nextern int divide(int a, int b, int *remainder);\nextern double avg(double *a, int n);\n\ntypedef struct Point {\n    double x,y;\n} Point;\n\nextern double distance(Point *p1, Point *p2);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u4e00\u70b9\u9700\u8981\u5f3a\u8c03\u7684\u662f\u8fd9\u4e9b\u58f0\u660e\u4f1a\u544a\u8bc9Swig\u4f60\u60f3\u8981\u5728Python\u6a21\u5757\u4e2d\u5305\u542b\u54ea\u4e9b\u4e1c\u897f\u3002\n\u901a\u5e38\u4f60\u9700\u8981\u7f16\u8f91\u8fd9\u4e2a\u58f0\u660e\u5217\u8868\u6216\u76f8\u5e94\u7684\u4fee\u6539\u4e0b\u5b83\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4e0d\u60f3\u67d0\u4e9b\u58f0\u660e\u88ab\u5305\u542b\u8fdb\u6765\uff0c\u4f60\u8981\u5c06\u5b83\u4ece\u58f0\u660e\u5217\u8868\u4e2d\u79fb\u9664\u6389\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528Swig\u6700\u590d\u6742\u7684\u5730\u65b9\u662f\u5b83\u80fd\u7ed9C\u4ee3\u7801\u63d0\u4f9b\u5927\u91cf\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002\n\u8fd9\u4e2a\u4e3b\u9898\u592a\u5927\uff0c\u8fd9\u91cc\u65e0\u6cd5\u5c55\u5f00\uff0c\u4f46\u662f\u6211\u4eec\u5728\u672c\u8282\u8fd8\u5269\u5c55\u793a\u4e86\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u4e1c\u897f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b2c\u4e00\u4e2a\u81ea\u5b9a\u4e49\u662f %extend \u6307\u4ee4\u5141\u8bb8\u65b9\u6cd5\u88ab\u9644\u52a0\u5230\u5df2\u5b58\u5728\u7684\u7ed3\u6784\u4f53\u548c\u7c7b\u5b9a\u4e49\u4e0a\u3002\n\u6211\u4f8b\u5b50\u4e2d\uff0c\u8fd9\u4e2a\u88ab\u7528\u6765\u6dfb\u52a0\u4e00\u4e2aPoint\u7ed3\u6784\u4f53\u7684\u6784\u9020\u5668\u65b9\u6cd5\u3002\n\u5b83\u53ef\u4ee5\u8ba9\u4f60\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528\u8fd9\u4e2a\u7ed3\u6784\u4f53\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p1 = sample.Point(2,3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u7565\u8fc7\u7684\u8bdd\uff0cPoint\u5bf9\u8c61\u5c31\u5fc5\u987b\u4ee5\u66f4\u52a0\u590d\u6742\u7684\u65b9\u5f0f\u6765\u88ab\u521b\u5efa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Usage if %extend Point is omitted\np1 = sample.Point()\np1.x = 2.0\np1.y = 3"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b2c\u4e8c\u4e2a\u81ea\u5b9a\u4e49\u6d89\u53ca\u5230\u5bf9 typemaps.i \u5e93\u7684\u5f15\u5165\u548c %apply \u6307\u4ee4\uff0c\n\u5b83\u4f1a\u6307\u793aSwig\u53c2\u6570\u7b7e\u540d int *remainder \u8981\u88ab\u5f53\u505a\u662f\u8f93\u51fa\u503c\u3002\n\u8fd9\u4e2a\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u6a21\u5f0f\u5339\u914d\u89c4\u5219\u3002\n\u5728\u63a5\u4e0b\u6765\u7684\u6240\u6709\u58f0\u660e\u4e2d\uff0c\u4efb\u4f55\u65f6\u5019\u53ea\u8981\u78b0\u4e0a int\u00a0 *remainder \uff0c\u4ed6\u5c31\u4f1a\u88ab\u4f5c\u4e3a\u8f93\u51fa\u3002\n\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u65b9\u6cd5\u53ef\u4ee5\u8ba9 divide() \u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u503c\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sample.divide(42,8)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u4e2a\u6d89\u53ca\u5230 %typemap \u6307\u4ee4\u7684\u81ea\u5b9a\u4e49\u53ef\u80fd\u662f\u8fd9\u91cc\u5c55\u793a\u7684\u6700\u9ad8\u7ea7\u7684\u7279\u6027\u4e86\u3002\n\u4e00\u4e2atypemap\u5c31\u662f\u4e00\u4e2a\u5728\u8f93\u5165\u4e2d\u7279\u5b9a\u53c2\u6570\u6a21\u5f0f\u7684\u89c4\u5219\u3002\n\u5728\u672c\u8282\u4e2d\uff0c\u4e00\u4e2atypemap\u88ab\u5b9a\u4e49\u4e3a\u5339\u914d\u53c2\u6570\u6a21\u5f0f (double *a, int n) .\n\u5728typemap\u5185\u90e8\u662f\u4e00\u4e2aC\u4ee3\u7801\u7247\u6bb5\uff0c\u5b83\u544a\u8bc9Swig\u600e\u6837\u5c06\u4e00\u4e2aPython\u5bf9\u8c61\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684C\u53c2\u6570\u3002\n\u672c\u8282\u4ee3\u7801\u4f7f\u7528\u4e86Python\u7684\u7f13\u5b58\u534f\u8bae\u53bb\u5339\u914d\u4efb\u4f55\u770b\u4e0a\u53bb\u7c7b\u4f3c\u53cc\u7cbe\u5ea6\u6570\u7ec4\u7684\u8f93\u5165\u53c2\u6570\n\uff08\u6bd4\u5982NumPy\u6570\u7ec4\u3001array\u6a21\u5757\u521b\u5efa\u7684\u6570\u7ec4\u7b49\uff09\uff0c\u66f4\u591a\u8bf7\u53c2\u800315.3\u5c0f\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728typemap\u4ee3\u7801\u5185\u90e8\uff0c$1\u548c$2\u8fd9\u6837\u7684\u53d8\u91cf\u66ff\u6362\u4f1a\u83b7\u53d6typemap\u6a21\u5f0f\u7684C\u53c2\u6570\u503c\n\uff08\u6bd4\u5982$1\u6620\u5c04\u4e3a double *a \uff09\u3002$input\u6307\u5411\u4e00\u4e2a\u4f5c\u4e3a\u8f93\u5165\u7684 PyObject * \u53c2\u6570\uff0c\n\u800c $argnum \u5c31\u4ee3\u8868\u53c2\u6570\u7684\u4e2a\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7f16\u5199\u548c\u7406\u89e3typemaps\u662f\u4f7f\u7528Swig\u6700\u57fa\u672c\u7684\u524d\u63d0\u3002\n\u4e0d\u4ec5\u662f\u8bf4\u4ee3\u7801\u66f4\u795e\u79d8\uff0c\u800c\u4e14\u4f60\u9700\u8981\u7406\u89e3Python C API\u548cSwig\u548c\u5b83\u4ea4\u4e92\u7684\u65b9\u5f0f\u3002\nSwig\u6587\u6863\u6709\u66f4\u591a\u8fd9\u65b9\u9762\u7684\u7ec6\u8282\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u6709\u5927\u91cf\u7684C\u4ee3\u7801\u9700\u8981\u88ab\u66b4\u9732\u4e3a\u6269\u5c55\u6a21\u5757\u3002\nSwig\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5173\u952e\u70b9\u5728\u4e8eSwig\u662f\u4e00\u4e2a\u5904\u7406C\u58f0\u660e\u7684\u7f16\u8bd1\u5668\uff0c\n\u901a\u8fc7\u5f3a\u5927\u7684\u6a21\u5f0f\u5339\u914d\u548c\u81ea\u5b9a\u4e49\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u8ba9\u4f60\u66f4\u6539\u58f0\u660e\u6307\u5b9a\u548c\u7c7b\u578b\u5904\u7406\u65b9\u5f0f\u3002\n\u66f4\u591a\u4fe1\u606f\u8bf7\u53bb\u67e5\u9605 Swig\u7f51\u7ad9 \uff0c\n\u8fd8\u6709 \u7279\u5b9a\u4e8ePython\u7684\u76f8\u5173\u6587\u6863"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}